/**
 * 费用设置 诊疗费设置
 */
<template>
  <div>
    <!-- 搜索筛选 -->
    <el-form :inline="true" :model="formInline" class="user-search">

      <el-form-item label="诊疗费名称">
        <el-input size="small" v-model="formInline.name" placeholder="输入诊疗费名称"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button size="small" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
        <el-button size="small" type="primary" icon="el-icon-plus" @click="handleEdit()">添加</el-button>
      </el-form-item>

    </el-form>
    <!--列表-->
    <el-table size="small" :data="listData" highlight-current-row v-loading="loading" border element-loading-text="拼命加载中" style="width: 100%;">
      <el-table-column sortable prop="id" label="序号" width="80">
      </el-table-column>
      <el-table-column sortable prop="name" label="诊疗费名称" width="120">
      </el-table-column>
      <el-table-column sortable prop="selling" label="金额（元）" width="120">
        <template slot-scope="scope">
          <!--将查到的价格(单位:分)转换单位(元)进行显示-->
          <div>{{ scope.row.selling / 100 }}</div>
        </template>
      </el-table-column>
      <el-table-column sortable prop="cost" label="成本价（元）" width="120">
        <template slot-scope="scope">
          <!--将查到的价格(单位:分)转换单位(元)进行显示-->
          <div>{{ scope.row.cost / 100 }}</div>
        </template>
      </el-table-column>
      <el-table-column sortable prop="createTime" label="创建时间" width="150">
      </el-table-column>
      <el-table-column sortable prop="creator" label="创建人" width="100">
      </el-table-column>
      <el-table-column sortable prop="costTypeLabel" label="费用状态" width="100">
      </el-table-column>

      <el-table-column align="center" label="操作" min-width="200">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="deleteOper(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <Pagination v-bind:child-msg="pageparm" @callFather="callFather"></Pagination>
    <!-- 编辑界面 -->
    <el-dialog :title="title" :visible.sync="editFormVisible" width="30%" @click="closeDialog">
      <el-form label-width="120px" :model="editForm" :rules="rules" ref="editForm">
        <el-form-item label="诊疗费名称" prop="name">
          <el-input size="small" v-model="editForm.name" auto-complete="off" placeholder="请输入诊疗费名称"></el-input>
        </el-form-item>
        <el-form-item label="金额(元)" prop="selling">
          <el-input-number size="small" :precision="2" :step="0.1" :min="0" v-model="editForm.selling" auto-complete="off" placeholder="请输入金额"></el-input-number>
        </el-form-item>
        <el-form-item label="成本价(元)" prop="cost">
          <el-input-number :precision="2" :step="0.1" :min="0" size="small" v-model="editForm.cost" auto-complete="off" placeholder="请输入成本价"></el-input-number>
        </el-form-item>
        <el-form-item label="创建人" prop="creator">
          <el-input size="small" v-model="editForm.creator" :disabled="isdisabledFn" auto-complete="off" placeholder="请输入创建人名称"></el-input>
        </el-form-item>
        <el-form-item label="费用状态" prop="costType">
          <el-select size="small" v-model="editForm.costType" placeholder="费用状态">                     
            <el-option v-for="(value,index) in costClassType" 
             :label="value" 
             :value="Number(index)" 
             :key="index">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeDialog">取消</el-button>
        <el-button size="small" type="primary" :loading="loading" class="title" @click="submitInsertForm('editForm')" v-if="title=='添加'">添加</el-button>
        <el-button size="small" type="primary" :loading="loading" class="title" @click="submitUpdateForm('editForm')" v-if="title=='修改'">修改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { costClassType } from '../../../dict/costClassType'
import { deptList, deptSave, deptDelete } from '../../../api/userMG'
import Pagination from '../../../components/Pagination'
import axios from 'axios'
export default {
  data() {
    return {
      nshow: true, //switch开启
      fshow: false, //switch关闭
      loading: false, //是显示加载
      isdisabledFn:true,
      editFormVisible: false, //控制编辑页面显示与隐藏
      title: '添加',
      editForm: {
        id:'',
        name:'', 
        selling:'',
        cost:'', 
        createTime:'',
        creator:'', 
        costType:'', 
        token: localStorage.getItem('logintoken')
      },
      costClassType:costClassType,
      // rules表单验证
      rules: {
        deptName: [
          { required: true, message: '请输入部门名称', trigger: 'blur' }
        ],
        deptNo: [{ required: true, message: '请输入部门代码', trigger: 'blur' }]
      },
    //   搜索数据
    //   searchForm:{
    //     page: 1,
    //     limit: 10,
    //     name: null,
    //     prescrptionType: null,
    //   },
      formInline: {
        page: 1,
        limit: 10,
        name:'',
        token: localStorage.getItem('logintoken')
      },
      // 删除部门
      seletedata: {
        ids: '',
        token: localStorage.getItem('logintoken')
      },
      userparm: [], //搜索权限
      listData: [], //附加费数据
      // 分页参数
      pageparm: {
        currentPage: 1,
        pageSize: 10,
        total: 10
      },
    }
  },
  // 注册组件
  components: {
    Pagination
  },

  created() {
    this.getdata(this.formInline);
  
  },

  /**
   * 里面的方法只有被调用才会执行
   */
  methods:  {
    // 获取诊疗费列表
    getdata(parameter) {
      this.loading = true      
      axios.post('/api/medical/selectAll/?pageNum='+parameter.page+'&pageSize='+parameter.limit,{'type':parameter.prescrptionType,'name':parameter.name})
        .then(res => {
          this.loading = false      
          this.listData = res.data.data.list
          for(let item of this.listData){
              item.costTypeLabel=this.costClassType[item.costType]
          }
          // 分页赋值
          this.pageparm.currentPage = this.formInline.page
          this.pageparm.pageSize = this.formInline.limit
          this.pageparm.total = res.data.data.total          
        })
        .catch(err => {
          this.loading = false
          this.$message.error('菜单加载失败，请稍后再试！')
        })
    },
    // 分页插件事件
    callFather(parm) {
      this.formInline.page = parm.currentPage
      this.formInline.limit = parm.pageSize
      this.getdata(this.formInline)
    },
    // 搜索事件
    search() {
      this.getdata(this.formInline)
    },
    //显示编辑界面
    handleEdit: function(index, row) {
      this.editFormVisible = true
      if (row != undefined && row != 'undefined') {
        this.title = '修改'
        this.editForm.id= row.id
        this.editForm.name= row.name
        this.editForm.selling= row.selling/100
        this.editForm.cost= row.cost/100
        this.editForm.createTime= row.createTime 
        this.editForm.creator= row.creator 
        this.editForm.costType= row.costType 
      } else {
        this.isdisabledFn=false
        this.title = '添加'
        this.editForm.id= ''
        this.editForm.name= ''
        this.editForm.selling= ''
        this.editForm.cost= '' 
        this.editForm.createTime= '' 
        this.editForm.creator= '' 
        this.editForm.costType= '' 
      }
    },
    // 新增弹窗的保存方法
    submitInsertForm(editData) {
      this.$refs[editData].validate(valid => {
        if (valid) {
          axios.post('/api/medical/insertOne',this.editForm)
            .then(res => {
              this.editFormVisible = false
              this.loading = false
              if (res.data.code==200) {
                this.getdata(this.formInline)
                this.$message({
                  type: 'success',
                  message: '数据保存成功！'
                })
              } else {
                this.$message({
                  type: 'info',
                  message: res.data.message
                })
              }
            })
            .catch(err => {
              this.editFormVisible = false
              this.loading = false
              this.$message.error('数据保存失败，请稍后再试！')
            })
        } else {
          return false
        }
      })
    },
    // 更新弹窗的保存方法
    submitUpdateForm(editData) {
      let form={};
      Object.assign(form,this.editForm);
      form.selling=form.selling*100
      form.cost=form.cost*100
      this.$refs[editData].validate(valid => {
        if (valid) {
          axios.post('/api/medical/updateById',form)
            .then(res => {
              this.editFormVisible = false
              this.loading = false
              if (res.data.code==200) {
                this.getdata(this.formInline)
                this.$message({
                  type: 'success',
                  message: '数据更新成功！'
                })
              } else {
                this.$message({
                  type: 'info',
                  message: res.data.message
                })
              }
            })
            .catch(err => {
              this.editFormVisible = false
              this.loading = false
              this.$message.error('数据更新失败，请稍后再试！')
            })
        } else {
          return false
        }
      })
    },
    // 删除附加费用
    deleteOper(index, row) {
      this.$confirm('确定要删除吗?', '信息', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          axios.get('/api/medical/deleteById/'+row.id)
          // deptDelete(row.deptId)
            .then(res => {
              if (res.data.code==200) {
                this.$message({
                  type: 'success',
                  message: '数据已删除!'
                })
                this.getdata(this.formInline)
              } else {
                this.$message({
                  type: 'info',
                  message: res.data.message
                })
              }
            })
            .catch(err => {
              this.loading = false
              this.$message.error('数据删除失败，请稍后再试！')
            })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 关闭编辑、增加弹出框
    closeDialog() {
      this.editFormVisible = false
    },
    
    // 处方类型框改变后调用
    selectChange(){
      this.getdata(this.formInline)      
    },
  }
}
</script>

<style scoped>
.user-search {
  margin-top: 20px;
}
.userRole {
  width: 100%;
}
</style>